package com.example.administrator.activityandintent1;

import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class Button_use extends AppCompatActivity {

    RadioButton r1=null;
    RadioButton r2=null;
    RadioButton r3=null;
    RadioButton r4=null;
    RadioGroup radioGroup=null;
    RadioButton currrentRadioButton=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取单选按钮组
        radioGroup = (RadioGroup)findViewById(R.id.radioGroup);

        //获取单选按钮
        r1 = (RadioButton)findViewById(R.id.a);
        r2 = (RadioButton)findViewById(R.id.b);
        r3 = (RadioButton)findViewById(R.id.c);
        r4 = (RadioButton)findViewById(R.id.d);
        r1.setClickable(true);
        Button btn1_commit = (Button)findViewById(R.id.right);
        Button btn2_cancle = (Button)findViewById(R.id.error);

        // 监听单选按钮
        radioGroup.setOnCheckedChangeListener(mChangeRadio);
        btn1_commit.setOnClickListener(new btn1_commit());
        btn2_cancle.setOnClickListener(new btn2_cancle());
    }

    private RadioGroup.OnCheckedChangeListener mChangeRadio = new RadioGroup.OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            if (checkedId == r1.getId())
                currrentRadioButton = r1;
            else if (checkedId == r2.getId())
                currrentRadioButton = r2;
            else if (checkedId == r3.getId())
                currrentRadioButton = r3;
            else if (checkedId == r4.getId())
                currrentRadioButton = r4;
        }
    };

    class btn1_commit implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            if (currrentRadioButton.getText().equals("in"))
                setTitle("You answer is Right!");
            else
                setTitle("You answer is wrong!");
        }
    }

    class btn2_cancle implements  View.OnClickListener{

        @Override
        public void onClick(View v) {
            radioGroup.clearCheck();
            setTitle("");
        }
    }
}



